package com.ruoyi.web.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.system.domain.PictureText;
import com.ruoyi.system.service.IPictureTextService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PictureRelationpicture;
import com.ruoyi.system.service.IPictureRelationpictureService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 图文关系管理Controller
 *
 * @author ruoyi
 * @date 2022-08-16
 */
@Controller
@RequestMapping("/system/relationpicture")
public class PictureRelationpictureController extends BaseController {
    private String prefix = "system/relationpicture";


    @Autowired
    private IPictureRelationpictureService pictureRelationpictureService;
    @Autowired
    private IPictureTextService pictureTextService;

    @RequiresPermissions("system:relationpicture:view")
    @GetMapping()
    public String relationpicture() {
        return prefix + "/relationpicture";
    }

    /**
     * 查询图文关系管理列表
     */
//    @RequiresPermissions("system:relationpicture:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PictureRelationpicture pictureRelationpicture) {
        startPage();
        List<PictureRelationpicture> list = pictureRelationpictureService.selectPictureRelationpictureList(pictureRelationpicture);
        return getDataTable(list);
    }

    /**
     * 导出图文关系管理列表
     */
    @RequiresPermissions("system:relationpicture:export")
    @Log(title = "图文关系管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PictureRelationpicture pictureRelationpicture) {
        List<PictureRelationpicture> list = pictureRelationpictureService.selectPictureRelationpictureList(pictureRelationpicture);
        ExcelUtil<PictureRelationpicture> util = new ExcelUtil<PictureRelationpicture>(PictureRelationpicture.class);
        return util.exportExcel(list, "图文关系管理数据");
    }

    /**
     * 新增图文关系管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存图文关系管理
     */
    @RequiresPermissions("system:relationpicture:add")
    @Log(title = "图文关系管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PictureRelationpicture pictureRelationpicture) {
        return toAjax(pictureRelationpictureService.insertPictureRelationpicture(pictureRelationpicture));
    }

    /**
     * 修改图文关系管理
     */
    @RequiresPermissions("system:relationpicture:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        PictureRelationpicture pictureRelationpicture = pictureRelationpictureService.selectPictureRelationpictureById(id);
        mmap.put("pictureRelationpicture", pictureRelationpicture);
        return prefix + "/edit";
    }

    /**
     * 修改保存图文关系管理
     */
    @RequiresPermissions("system:relationpicture:edit")
    @Log(title = "图文关系管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PictureRelationpicture pictureRelationpicture) {
        return toAjax(pictureRelationpictureService.updatePictureRelationpicture(pictureRelationpicture));
    }

    /**
     * 删除图文关系管理
     */
    @RequiresPermissions("system:relationpicture:remove")
    @Log(title = "图文关系管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(pictureRelationpictureService.deletePictureRelationpictureByIds(ids));
    }

    /**
     * 自己的添加图文关系
     *
     * @param pictureRelationpicture
     * @return
     */
    @PostMapping("/myadd")
    @ResponseBody
    public AjaxResult myadd(PictureRelationpicture pictureRelationpicture) {
        PictureRelationpicture item=new PictureRelationpicture();
        item.setPictureId(pictureRelationpicture.getPictureId());
        item.setUserId(pictureRelationpicture.getUserId());
      if (pictureRelationpicture.getUpStatus()==0&&pictureRelationpicture.getCollectStatus()==0){
          PictureRelationpicture newpictureRelationpicture=new PictureRelationpicture();
          newpictureRelationpicture.setUserId(pictureRelationpicture.getUserId());
          newpictureRelationpicture.setPictureId(pictureRelationpicture.getPictureId());
          Long id = pictureRelationpictureService.selectPictureRelationpictureList(newpictureRelationpicture).get(0).getId();
          pictureRelationpictureService.deletePictureRelationpictureById(id);

      }else if (pictureRelationpictureService.selectPictureRelationpictureList(item).size()==0){
          pictureRelationpictureService.insertPictureRelationpicture(pictureRelationpicture);
      }else {
          PictureRelationpicture newpictureRelationpicture=new PictureRelationpicture();
          newpictureRelationpicture.setUserId(pictureRelationpicture.getUserId());
          newpictureRelationpicture.setPictureId(pictureRelationpicture.getPictureId());
          newpictureRelationpicture.setUpStatus(pictureRelationpicture.getUpStatus());
          newpictureRelationpicture.setCollectStatus(pictureRelationpicture.getCollectStatus());
          Long id = pictureRelationpictureService.selectPictureRelationpictureList(newpictureRelationpicture).get(0).getId();
          newpictureRelationpicture.setId(id);
          pictureRelationpictureService.updatePictureRelationpicture(newpictureRelationpicture);
      }
        PictureText pictureText=new PictureText();
        pictureText.setId(pictureRelationpicture.getPictureId());
        PictureRelationpicture newpictureRelationpicture=new PictureRelationpicture();
        newpictureRelationpicture.setPictureId(pictureRelationpicture.getPictureId());
        newpictureRelationpicture.setCollectStatus((long)1);
        Long CollectCount=(long)pictureRelationpictureService.getupStatusAndcollectStatusList(newpictureRelationpicture).size();
        pictureText.setCollectCount(CollectCount);
        newpictureRelationpicture=new PictureRelationpicture();
        newpictureRelationpicture.setPictureId(pictureRelationpicture.getPictureId());
        newpictureRelationpicture.setUpStatus((long)1);
        Long UpCount=(long)pictureRelationpictureService.getupStatusAndcollectStatusList(newpictureRelationpicture).size();
        pictureText.setUpCount(UpCount);
        System.out.println(pictureRelationpictureService.getupStatusAndcollectStatusList(newpictureRelationpicture));
        pictureTextService.updatePictureText(pictureText);
        return success();
    }

    //这个要修改一下，变成查询关注和收藏列表

    /**
     * 查询关注类别，可以查询所有也可以查询单个
     *
     * @param pictureRelationpicture
     * @return
     */
    @PostMapping("/mylist")
    @ResponseBody
    public AjaxResult getList(PictureRelationpicture pictureRelationpicture) {
        return success(pictureRelationpictureService.getupStatusAndcollectStatusList(pictureRelationpicture).size());
    }
    @PostMapping("/getentitylist")
    @ResponseBody
    public AjaxResult getEntityList(PictureRelationpicture pictureRelationpictur){
        List<PictureText> pictureTextList=new ArrayList<>();
        List<PictureRelationpicture> list = pictureRelationpictureService.selectPictureRelationpictureList(pictureRelationpictur);
        for (PictureRelationpicture item : list) {
            PictureText picItem = pictureTextService.selectPictureTextById(item.getPictureId());
            pictureTextList.add(picItem);
        }
        return success(pictureTextList);

    }

}
